﻿using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Helper class for Navigation
/// </summary>

namespace si.caha.Helper
{
    public class Navigator
    {
        private static string[] ExcludedFromNavigationMenu = new string[] { "Delete", "Edit", "Item", "Price", "Page", "Details" };

        public static List<Dictionary<string, string>> GetNavigationUrls(string Url)
        {
            int indexOfQuestionMark = Url.IndexOf("?");
            List<string> urlItems = new List<string>();

            // don't include url parameters in navigation
            if (indexOfQuestionMark >= 0)
                urlItems = Url.Substring(0, Url.IndexOf("?")).Split('/').ToList();
            else
                urlItems = Url.Split('/').ToList();

            urlItems.RemoveRange(0, 3); // remove protocol, empty space and doman with port
            urlItems.Remove("");        // remove any empty spaces

            List<Dictionary<string, string>> navigationUrls = new List<Dictionary<string, string>>();
            string navigationString = "~";
            foreach (String urlItem in urlItems)
            {
                if (ExcludedFromNavigationMenu.Contains(urlItem))
                    break;

                // don't include id's in menu
                int id;
                if (Int32.TryParse(urlItem, out id))
                    break;

                var formattedUrlItem = urlItem;
                // if file has extension
                if (urlItem.Contains("."))
                    formattedUrlItem = urlItem.Substring(0, urlItem.IndexOf("."));

                navigationString += "/" + formattedUrlItem;
                Dictionary<string, string> urlDictionary = new Dictionary<string, string>();
                urlDictionary.Add("Url", navigationString);
                urlDictionary.Add("Title", formattedUrlItem.Replace("-", " "));
                navigationUrls.Add(urlDictionary);
            }

            return navigationUrls;
        }
    }
}